
auto.waitFor();

// 判断设备是否锁屏
var isScreenOn = device.isScreenOn();
// 解锁密码，根据实际修改
var password = "698745";

// 各数字的坐标，需要根据实际屏幕调整！！！！！
var keyPositions = {
    "1": [320, 1760],
    "2": [630, 1760],
    "3": [950, 1760],
    "4": [320, 1980],
    "5": [630, 1980],
    "6": [950, 1980],
    "7": [320, 2230],
    "8": [630, 2230],
    "9": [950, 2230],
    "0": [630, 2470],
};

if(!isScreenOn){ //如果锁屏
    // 唤醒设备
    device.wakeUpIfNeeded();
    // 等待屏幕响应
    sleep(300);

    // 从底部向上滑动（调出密码界面）
    var startY = device.height - 100;
    var endY = 100;
    swipe(device.width / 2, startY, device.width / 2, endY, 500);
    sleep(300);

    // 输入密码
    for (var i = 0; i < password.length; i++) {
        var num = password.charAt(i);
        var pos = keyPositions[num];
        if (pos) {
            click(pos[0], pos[1]);
            sleep(50); // 根据输入响应速度调整
        }
    }

    // 等待解锁完成
    sleep(1000);
}

// 打开支付宝
launchApp("支付宝");
toast("等待支付宝启动...");
sleep(3000);

let executionCount = 0;
enterForest();

// 进入蚂蚁森林
function enterForest() {
    click(150, 900);
    toast("进入蚂蚁森林...");
    sleep(5000);
    // todo ...

    //切换账号
    executionCount++;
    click(1144, 2710);
    sleep(1000);
    click(1184, 246);
    sleep(1000);
    click(660, 2190);
    sleep(1000);
    click(660, 2043);
    sleep(1000);

    console.log("执行次数", executionCount);
    if (executionCount < 4) {
        enterForest(); // 递归执行
    } else {
        toast("已完成4次执行");
        home();
        sleep(3000);
        //自动息屏
        runtime.accessibilityBridge.getService().performGlobalAction(android.accessibilityservice.AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN);
        exit();
    }

}

